package com.hsgene.user.converter;

import com.hsgene.common.util.basic.UUID32;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.common.util.web.session.UserInfoExtra;
import com.hsgene.user.constants.CustomerSourceType;
import com.hsgene.user.domain.GeneticTestingOperationAccount;
import com.hsgene.user.domain.GeneticTestingUser;
import com.hsgene.user.domain.User;
import com.hsgene.user.domain.UserClass;
import com.hsgene.user.dto.user.GeneticTestingUserDto;
import com.hsgene.user.dto.user.customer.CustomerDto;
import com.hsgene.user.dto.user.manager.ManagerDto;
import com.hsgene.user.dto.user.manager.OperationAccountExtraDto;
import com.hsgene.user.session.UserSessionComponent;
import org.springframework.beans.BeanUtils;

/**
 * 用户相关转换类
 *
 * @author wangbing
 * @version 1.0, 2018/7/23
 */
public class UserConverter {

    /**
     * 从UserDto中构建UserInfo
     *
     * @param user UserDto
     * @return UserInfo
     */
    public static UserInfo buildUserInfo(CustomerDto user, CustomerSourceType sourceType) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getUserId());
        userInfo.setUserPhone(user.getPhone());
        userInfo.setUserType(user.getUserType());
        userInfo.setShowName(user.getNickName());
        userInfo.setShowHead(user.getHeadImage());
        userInfo.setSessionId(UserSessionComponent.buildCustomerSessionId(sourceType, userInfo));
        return userInfo;
    }

    /**
     * 从ManagerDto中构建UserInfo
     *
     * @param manager ManagerDto
     * @return UserInfo
     */
    public static UserInfo buildUserInfo(ManagerDto manager) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(manager.getUserId());
        userInfo.setUserType(manager.getUserType());
        if (manager.getNormal()) {
            userInfo.setUserPhone(manager.getNormalManager().getPhone());
            userInfo.setShowName(manager.getNormalManager().getNickName());
            userInfo.setShowHead(manager.getNormalManager().getHeadImage());
            userInfo.setRole(manager.getNormalManager().getRoleType());
            userInfo.setSupplierId(manager.getNormalManager().getSupplierId());
        } else {
            userInfo.setUserPhone(manager.getSuperManager().getUserName());
            userInfo.setShowName(manager.getSuperManager().getUserName());
            userInfo.setSupplierId(manager.getSuperManager().getSupplierId());
            userInfo.setRole("admin");
        }
        userInfo.setSessionId(UserSessionComponent.buildSessionId(userInfo));

        return userInfo;
    }

    /**
     * 从GeneticTestingUserDto中构建UserInfo
     *
     * @param user GeneticTestingUserDto
     * @return UserInfo
     */
    public static UserInfo buildUserInfo(GeneticTestingUserDto user, CustomerSourceType sourceType) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setUserPhone(user.getPhone());
        userInfo.setUserType(user.getUserType().toString());
        userInfo.setShowName(user.getNick());
        String showHead = user.getAvatar() == null ? user.getDefaultAvatar() : user.getAvatar();
        userInfo.setShowHead(showHead);
        userInfo.setRealName(user.getRealName());
        userInfo.setSessionId(UserSessionComponent.buildCustomerSessionId(sourceType, userInfo));
        if ("0".equals(userInfo.getUserType()) || "11".equals(userInfo.getUserType())) {
            UserInfoExtra extra = new UserInfoExtra();
            BeanUtils.copyProperties(user.getExtra(), extra);
            userInfo.setExtra(extra);
        }

        return userInfo;
    }

    /**
     * 构建CustomerDto
     *
     * @param user      用户账号基础信息
     * @param userClass 用户账号分类信息
     * @return CustomerDto
     */
    public static CustomerDto buildCustomerDto(User user, UserClass userClass) {
        CustomerDto customer = new CustomerDto();
        BeanUtils.copyProperties(user, customer);
        BeanUtils.copyProperties(userClass, customer);
        // 置空，不返回password字段值
        customer.setPassword(null);
        return customer;
    }

    /**
     * GeneticTestingUser -> GeneticTestingUserDto
     *
     * @param user GeneticTestingUser
     * @return GeneticTestingUserDto
     */
    public static GeneticTestingUserDto toGeneticTestingUserDto(GeneticTestingUser user) {
        GeneticTestingUserDto dto = new GeneticTestingUserDto();
        BeanUtils.copyProperties(user, dto);
        dto.setPassword(null);
        return dto;
    }

    /**
     * GeneticTestingOperationAccount -> GeneticTestingUserDto
     *
     * @param operationAccount GeneticTestingOperationAccount
     * @return GeneticTestingUserDto
     */
    public static GeneticTestingUserDto toGeneticTestingUserDto(GeneticTestingOperationAccount operationAccount) {
        GeneticTestingUserDto dto = new GeneticTestingUserDto();
        BeanUtils.copyProperties(operationAccount, dto);
        if (operationAccount.getUserType() != null) {
            if (0 == operationAccount.getUserType()) {
                dto.setUserType(0);
            } else {
                dto.setUserType(11);
            }
        }
        dto.setPassword(null);

        OperationAccountExtraDto extra = new OperationAccountExtraDto();
        BeanUtils.copyProperties(operationAccount, extra);
        dto.setExtra(extra);

        return dto;
    }
}
